package com.muyu.system.service.impl;


import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.system.domain.DanmoProductFootprint;
import com.muyu.system.domain.DanmoShoppingFlagship;
import com.muyu.system.mapper.DanmoProductFootprintMapper;
import com.muyu.system.mapper.DanmoShoppingFlagshipMapper;
import com.muyu.system.req.DanmoShoppingFlagshipReq;
import com.muyu.system.req.DanmoShoppingProductsPriceReq;
import com.muyu.system.service.IDanmoShoppingFlagshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 旗舰店Service业务层处理
 *
 * @author yangle
 * @date 2024-09-29
 */
@Service
public class DanmoShoppingFlagshipServiceImpl implements IDanmoShoppingFlagshipService
{
    @Autowired
    private DanmoShoppingFlagshipMapper danmoShoppingFlagshipMapper;

    @Autowired
    private DanmoProductFootprintMapper danmoProductFootprintMapper;
    /**
     * 查询旗舰店
     *
     * @param id 旗舰店主键
     * @return 旗舰店
     */
    @Override
    public DanmoShoppingFlagship selectDanmoShoppingFlagshipById(Long id)
    {
        Long userid = SecurityUtils.getLoginUser().getUserid();
        // 查询店铺足迹
        List<DanmoProductFootprint> danmoProductFootprintList = danmoProductFootprintMapper.selectFlagship(userid, id);

        // 判断店铺中是否有该店铺的足迹
        if (danmoProductFootprintList != null && !danmoProductFootprintList.isEmpty()) {
            // 遍历列表，更新每个足迹的更新时间
            for (DanmoProductFootprint footprint : danmoProductFootprintList) {
                footprint.setUpdateTime(DateUtils.getNowDate());
                danmoProductFootprintMapper.updateDanmoProductFootprint(footprint);
            }
        } else {
            DanmoProductFootprint build = DanmoProductFootprint.builder()
                    .userId(userid)
                    .shopsId(id)
                    .browseTime(DateUtils.getNowDate())
                    .types(2L)
                    .createTime(DateUtils.getNowDate())
                    .build();
            danmoProductFootprintMapper.insertDanmoProductFootprint(build);
        }

        return danmoShoppingFlagshipMapper.selectDanmoShoppingFlagshipById(id);
    }

    /**
     * 查询旗舰店列表
     *
     * @param danmoShoppingFlagship 旗舰店
     * @return 旗舰店
     */
    @Override
    public List<DanmoShoppingFlagship> selectDanmoShoppingFlagshipList(DanmoShoppingFlagship danmoShoppingFlagship)
    {
        return danmoShoppingFlagshipMapper.selectDanmoShoppingFlagshipList(danmoShoppingFlagship);
    }

    /**
     * 新增旗舰店
     *
     * @param danmoShoppingFlagship 旗舰店
     * @return 结果
     */
    @Override
    public int insertDanmoShoppingFlagship(DanmoShoppingFlagship danmoShoppingFlagship)
    {
        danmoShoppingFlagship.setCreateTime(DateUtils.getNowDate());
        return danmoShoppingFlagshipMapper.insertDanmoShoppingFlagship(danmoShoppingFlagship);
    }

    /**
     * 修改旗舰店
     *
     * @param danmoShoppingFlagship 旗舰店
     * @return 结果
     */
    @Override
    public int updateDanmoShoppingFlagship(DanmoShoppingFlagship danmoShoppingFlagship)
    {
        danmoShoppingFlagship.setUpdateTime(DateUtils.getNowDate());
        return danmoShoppingFlagshipMapper.updateDanmoShoppingFlagship(danmoShoppingFlagship);
    }

    /**
     * 批量删除旗舰店
     *
     * @param ids 需要删除的旗舰店主键
     * @return 结果
     */
    @Override
    public int deleteDanmoShoppingFlagshipByIds(Long[] ids)
    {
        return danmoShoppingFlagshipMapper.deleteDanmoShoppingFlagshipByIds(ids);
    }

    /**
     * 删除旗舰店信息
     *
     * @param id 旗舰店主键
     * @return 结果
     */
    @Override
    public int deleteDanmoShoppingFlagshipById(Long id)
    {
        return danmoShoppingFlagshipMapper.deleteDanmoShoppingFlagshipById(id);
    }

    @Override
    public List<DanmoShoppingFlagship> listProducts(DanmoShoppingFlagshipReq danmoShoppingFlagshipReq) {
        return danmoShoppingFlagshipMapper.listProducts(danmoShoppingFlagshipReq);
    }

    /**
     * 旗舰店知名度排序
     * @param danmoShoppingProductsPriceReq
     * @return
     */
    @Override
    public List<DanmoShoppingFlagship> selectDanmoShoppingFlagshipListRankingOfPopularity(DanmoShoppingProductsPriceReq danmoShoppingProductsPriceReq) {
        return danmoShoppingFlagshipMapper.selectDanmoShoppingFlagshipListRankingOfPopularity(danmoShoppingProductsPriceReq);
    }
}
